package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.context.BaseContext;
import com.honglv.mapper.CommodityMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UpdateCommodityDTO;
import com.honglv.pojo.dto.UploadCommodityDTO;
import com.honglv.pojo.entity.Commodities;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllCommoditiesVO;
import com.honglv.pojo.vo.GetCommoditiesByCityVO;
import com.honglv.pojo.vo.GetCommodityVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class CommodityServiceImpl  implements CommodityService{

        @Autowired
        private CommodityMapper commodityMapper;
        /**
         * 管理员上传文创
         * @param
         * @return
         */
        @Override
        public void upload(UploadCommodityDTO uploadCommodityDTO) {
            Long authorId = BaseContext.getCurrentId();
            Commodities commodities=new Commodities();
            BeanUtils.copyProperties(uploadCommodityDTO,commodities);
            commodities.setAuthorId(authorId);
            commodityMapper.insert(commodities);


        }
        /**
         * 根据城市分页获取文创
         * @param
         * @return
         */
        @Override
        public PageVO<GetCommoditiesByCityVO> getCommoditiesByCity(PageByCityDTO pageByCityDTO) {
            PageHelper.startPage(pageByCityDTO.getPage(), pageByCityDTO.getPageSize());
            QueryWrapper<Commodities> qw = new QueryWrapper<>();
            qw.like("city", pageByCityDTO.getCity());
            List<Commodities> list = commodityMapper.selectList(qw);
            GetCommoditiesByCityVO gf = new GetCommoditiesByCityVO();
            List<GetCommoditiesByCityVO> commodities = new ArrayList<>();
            for (Commodities Commodities : list) {
                BeanUtils.copyProperties(Commodities, gf);
                commodities.add(gf);
            }
            if (commodities == null) {
                return null;
            }
            PageInfo<GetCommoditiesByCityVO> pageInfo = new PageInfo<>(commodities);
            return new PageVO<>(pageInfo.getTotal(), pageInfo.getList());


        }

            /**
             * Description:分页获取文创
             * date: 2024/04/01
             * author:王铭杰
             */
            public PageVO<GetAllCommoditiesVO> getAllCommodities(PageDTO pageDTO) {
                PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
                List<GetAllCommoditiesVO> list = commodityMapper.getAllCommodities();
                if (list == null){
                    return null;
                }
                PageInfo<GetAllCommoditiesVO> pageInfo = new PageInfo<>(list);
                return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
            }


            /**
             * Description:获取文创详情页
             * date: 2024/04/01
             * author:王铭杰
             */
            public GetCommodityVO getCommodity(Long id) {
                GetCommodityVO getCommodityVO = commodityMapper.getCommodity(id);
                return getCommodityVO;
            }

            /**
             * Description:通过用户id获取文创
             * date: 2024/04/01
             * author:王铭杰
             */
            @Override
            public List<GetAllCommoditiesVO> getCommoditiesByAuthorId(Long userId) {
                List<GetAllCommoditiesVO> list = commodityMapper.getCommoditiesByAuthorId(userId);
                return list;
            }

            @Override
            public Result deleteCommodity(Long id) {
                commodityMapper.deleteCommodity(id);
                return Result.success("删除成功");
            }

            @Override
            public Result updateCommodity(UpdateCommodityDTO updateCommodityDTO) {
                if(commodityMapper.updateCommodity(updateCommodityDTO) > 0){
                    return Result.success("修改成功！");
                }
                return Result.success("修改失败，未知错误!");
            }
}
